package com.ruoyi;

import com.ruoyi.activiti.modeler.support.UserTaskParseHandlerSupport;
import org.activiti.engine.parse.BpmnParseHandler;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zhenghailong
 * @since create in 2021/1/12 21:13
 */
@Configuration
public class ProcessConfiguration {
    @Bean(name = "processEngineFactory")
    public ProcessEngineFactoryBean processEngineFactory(SpringProcessEngineConfiguration processEngineConfiguration) {
        ProcessEngineFactoryBean factory = new ProcessEngineFactoryBean();
        factory.setProcessEngineConfiguration(processEngineConfiguration);
        return factory;
    }

    @Bean(name = "processEngineConfiguration")
    public SpringProcessEngineConfiguration processEngineConfiguration(@Value("${jdbc.dbType:mysql}") String dbType,
                                                                       @Value("${lmbus-activiti.fontName:simsun}") String fontName,
                                                                       @Value("${lmbus-activiti.databaseSchemaUpdate:false}") String databaseSchemaUpdate,
                                                                       @Value("${lmbus-activiti.databaseCatalog:}") String databaseCatalog,
                                                                       @Value("${jdbc.url:}") String url,
                                                                       @Autowired DataSource dataSource,
                                                                       @Autowired PlatformTransactionManager transactionManager) {

        if(StringUtils.isEmpty(databaseCatalog) && !StringUtils.isEmpty(url)) {
            if(url.startsWith("jdbc:")) {
                url = url.substring(5);
            }
            URI uri = URI.create(url);
            String path = uri.getPath();
            if(path != null && path.charAt(0) == '/') {
                path = path.substring(1);
            }
            databaseCatalog = path;
        }
        SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
        spec.setDatabaseCatalog(databaseCatalog);
        spec.setDatabaseType(dbType);
        spec.setActivityFontName(fontName);
        spec.setLabelFontName(fontName);
        spec.setAnnotationFontName(fontName);
        spec.setDatabaseSchemaUpdate(databaseSchemaUpdate);
        spec.setDataSource(dataSource);
        spec.setTransactionManager(transactionManager);
//        spec.setActivityBehaviorFactory(activityBehaviorFactory);
        spec.setProcessEngineName("MyProcessEngine");
//        spec.setScriptingEngines(GroovyScriptingEngines.create(scriptManager));
        //spec.setExpressionManager(new LooseExpressionManager());
        //创建自定义的Generator类：流程图
//        spec.setProcessDiagramGenerator(new CustomizedProcessDiagramGenerator());

        List<BpmnParseHandler> customDefaultBpmnParseHandlers = new ArrayList<>();
        customDefaultBpmnParseHandlers.add(new UserTaskParseHandlerSupport());
        spec.setCustomDefaultBpmnParseHandlers(customDefaultBpmnParseHandlers);

//        int maxLen = spec.getMaxVarcharLength();
//        final int minLen = 512;
//        //长度小于512的字符串保存在text_
//        spec.setMaxLengthStringVariableType(minLen);
//        Text2StringType stringType = new Text2StringType(minLen, maxLen);
//        spec.setCustomPreVariableTypes(Arrays.asList(stringType));
        return spec;
    }
}
